package com.tmusoft.service.impl;

import cn.hutool.core.util.IdUtil;
import com.tmusoft.domain.AjaxResult;
import com.tmusoft.service.FileService;
import com.tmusoft.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.*;

@Service
public class FileServiceImpl implements FileService {

    @Value("${upload.path}")
    public String FILE_PATH;

    @Value("${upload.prefix}")
    public String domain;

    @Value("#{'${upload.exclude}'.split(',')}")
    private List<String> excludeList;

    @Override
    public AjaxResult uploadFile(MultipartFile multipartFile, String type, String folder, String token) {
        try {
            if (multipartFile.isEmpty()) {
                return AjaxResult.error("文件不能为空");
            } else if (type == null) {
                return AjaxResult.error("文件不能为空");
            } else if (StringUtils.isEmpty(token)) {
                return AjaxResult.error("上传密钥非法");
            }

            if (!"352a4887aa12abadf9b6f39a7497b929".equals(token)) {
                return AjaxResult.error("token非法");
            }

            // 上传简单文件名
            String originalFilename = multipartFile.getOriginalFilename();
            //文件后缀名
            String prefix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            boolean contains = excludeList.contains(prefix);
            if(!contains){
                return AjaxResult.error("不允许的文件格式");
            }
            String fileName = originalFilename.substring(0, originalFilename.lastIndexOf(".")) + "_" + IdUtil.objectId() +"." + prefix;

            StringBuilder filePath = new StringBuilder();
            StringBuilder url = new StringBuilder();
            String date = DateUtil.format(new Date(),"yyyyMMdd");
            url.append(domain);
            switch (type) {
                case "1":
                    filePath.append("image/");
                    url.append("image/");
                    break;
                case "2":
                    filePath.append("file/");
                    url.append("file/");
                    break;
            }
            filePath.append(date);
            filePath.append("/");
            if (StringUtils.isNotEmpty(folder)) {
                filePath.append(folder);
                filePath.append("/");
            }
            filePath.append(fileName);

            url.append(date);
            url.append("/");
            if (StringUtils.isNotEmpty(folder)) {
                url.append(folder);
                url.append("/");
            }
            url.append(fileName);
            File file = new File(FILE_PATH + filePath.toString());
            if (!file.exists()) {
                file.mkdirs();
            }
            multipartFile.transferTo(file);
            Map<String, Object> resData = new HashMap<>();
            resData.put("path", filePath);
            resData.put("prefix", prefix);
            resData.put("originalName", originalFilename);
            resData.put("url", url);
            resData.put("size", multipartFile.getSize());
            return AjaxResult.success(resData);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
